<?php
class partnerController extends base
{
    public function __construct()
    {
        parent::__construct('outward');
    }
    private function verifySign()
    {
        $result = FALSE;
        $sign = common::request('sign', 'G');
        if($sign)
        {
            $params = array_merge($_GET, $_POST);
            unset($params['sign'],$params['c'],$params['a']);
            ksort($params);
            $serverSign = md5(urldecode(http_build_query($params, NULL, '&')) . '***O5OQnIPVV8y1cBmLn8yeaH58QjJ2kHLxB-mbcG5R***');
            //$this->loginfo("sign=========".$serverSign."===".$sign);
            $serverSign == $sign && $result = TRUE;
        }
        if($result == FALSE)
        {
            $this->load('partner')->getMsg(30000);
            exit;
        }
        return ;
    }
    /**
     *   绑定
     */
    public function dobind()
    {
        $this->in_verifySign();
        $this->in_client(TRUE);
        $appid = common::request('appid', 'G');
        $appkey = common::request('appkey', 'G');
        $callback = common::request('callback', 'G');
        $puid = common::request('puid', 'G');
        $this->loginfo("bind 66666.======".$puid);
        $status = 0;
        if(!$appid || !$appkey || !$puid)
        {
            $this->load('error')->getErrors(101);
            exit;
        }
        if($this->load('partner')->checkPartner($appid, $appkey) == false)
        {
            $this->load('error')->getErrors(101);
            exit;
        }
        $uid = $this->uid;
        if(!$uid)
        {
            $this->load('error')->getErrors(201);
            exit;
        }
        $isBind = $this->load('partner')->checkPartnerBind($appid, $uid, $puid);
        if($isBind['checkUid'] > 0)
        {
            if($isBind['checkUid'] == $uid)
            {
                $status = 1;
            }
            else
            {
                // 第三方账号已与其他用户绑定
                $status = 2;
            }
        }
        if($isBind['checkpuid'])
        {
            if($isBind['checkpuid'] == urldecode($puid))
            {
                $status = 1;
            }
            else
            {
                // 用户已与其他第三方账号绑定
                $status = 3;
            }
        }
        if($status == 0)
        {
            if($this->load('partner')->bind($appid, $uid, $puid))
            {
                $status = 1;
            }
        }
        $this->loginfo("1.======".$appid."===".$puid."===".$uid);
        $status == 1 && $this->load('partner')->bindCallback($appid, $puid, $uid);
        $this->result['result']['bindStatus'] = $status;
    }


    private function loginfo($content){
        //文件所处目录
        $path = $this->config['log-dir'].'partner/';
        //查看文件目录是否存在,不存在创建
        !file_exists($path) && mkdir($path);
        //文件名称及类型
        $logName = date('Ymd').'.txt';
        //写入方式打开，将文件指针指向文件末尾。如果文件不存在则尝试创建之。
        $fp = fopen($path.$logName, "a");
        //文件内容前缀
        $log_prefix = '---start---'.date('Y-m-d H:i:s')."---\n";
        //文件内容后缀
        $log_suffix = '---end---'.date('Y-m-d H:i:s')."---\n";
        //写入内容
        fwrite($fp, $log_prefix . var_export($content,true) . "\n" . $log_suffix);
        //写入换行
        fwrite($fp, "\n");
        //关闭
        fclose($fp);
    }

    /**
     * 解除绑定
     */
    public function dounbind()
    {
        $this->verifySign();

        $appid = common::request('appid', 'G');
        $appkey = common::request('appkey', 'G');
        $puid = common::request('partnerUserId', 'G');
        $uid = common::request('runUserId', 'G');

        $puid = urlencode($puid);
        $this->loginfo("99991.======".$puid);
        $this->loginfo("99992.======".urlencode($puid));
        if(!$appid || !$appkey)
        {
            $this->load('partner')->getMsg(30001);
            exit;
        }
        if(!$uid || !$puid)
        {
            $error = !$uid ? 10004 : 10005;
            $this->load('partner')->getMsg($error);
            exit;
        }
        $isBind = $this->load('partner')->checkPartnerBind($appid, $uid, $puid);

        $this->loginfo("0000.======".var_export($isBind,true).";===".$isBind['checkUid']."=".$uid.";".$isBind['checkpuid']."=".$puid);
        if($isBind['checkUid'] != $uid || $isBind['checkpuid'] != urldecode($puid))
        {
            $this->load('partner')->getMsg(20002);
            exit;
        }
        if(!$this->load('partner')->unbind($appid, $uid, $puid))
        {
            $this->load('partner')->getMsg(10006);
            exit;
        }
        $this->load('partner')->getMsg(10000);
        $this->result['runUserId'] = $uid;
    }
    
    /*
     * 阿里体育报名通知
     */
    public function doentrollCallback(){
        $uid = common::request('userid','P');
        $partner_uid = common::request('aliuid','P');
        $appkey = common::request('appkey','P');
        $partner_eid = common::request('eid','P');
        $signid = common::request('signid','P');
        $sign_time = common::request('sign_time','P');
        $md5 = common::request('md5','P');
        
        $partner_mod = $this->load('alibaba');
        
        if(!$partner_mod->verify($appkey,$md5,'post')){
            $this->load('error')->getErrors(102);
        }
        
        
        $userInfo = $this->load('client')->getUserInfo($uid);
        if(!$userInfo){
            $this->load('error')->getErrors(309);
        }
        
        //bind
        $addtime = $sign_time ? $sign_time : time();
        $partner_mod->bind(compact('uid','partner_uid','partner_eid','addtime'));
        //callback
        $userInfo['partner_uid'] = $partner_uid;
        $partner_mod->updateMember($userInfo);
    }
    
    /*
     * 批量同步用户
     */
    public function dobatchUserSync(){
        set_time_limit(600);
        $uids = common::request('userid[]','P');
        $page_no = common::request('page_no','P');
        $total = common::request('total','P');
        $md5 = common::request('md5','P');
        $appkey = common::request('appkey','P');
        
        $partner_mod = $this->load('alibaba');
        if(!$partner_mod->verify($appkey,$md5,'post')){
            $this->load('error')->getErrors(102);
        }
        
        if(!$uids){
            $this->load('error')->getErrors(309);
        }
        
        $client_mod = $this->load('client');
        foreach($uids as $uid){
            $userInfo = $clien_mod->getUserInfo($uid);
            $partner_mod->updateMember($userInfo);
        }
    }
    
    /*
     * 获取用户信息
     */
    public function dogetCoins(){
        $this->in_user();
        $uid = $this->uid;
        if(!$uid){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $res = $this->load('alibaba')->getCoins($uid);
        if(!$res){
            $_ENV['error']->getErrors(100);
            exit;
        }
        $this->result['result'] = $res;
    }
}